package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.entity.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    //注入商品服务的远程调用接口
    @Autowired
    ProductFeignService productFeignService;

    /**
     * 条件查询 根据skuId 和 wareId查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId) && !"0".equalsIgnoreCase(skuId)){
            wrapper.eq("sku_id",skuId);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId) && !"0".equalsIgnoreCase(wareId)){
            wrapper.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据skuId wareId skuNum更新库存
     * @param skuId 商品Id
     * @param wareId 仓库Id
     * @param skuNum 新增商品数量
     *
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {

        //判断库存中是否有该条记录 如果没有则新增 如果有就修改
        List<WareSkuEntity> list = this.list(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (list == null || list.size() == 0){
            System.out.println("新增WareSkuEntity--");
            //查无此记录 新增
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            //默认不锁定库存
            wareSkuEntity.setStockLocked(0);
            //注入商品服务的远程调用接口productFeignService
            //这里仅仅远程调用服务获取一个skuName 如果失败抛出异常后回滚得不偿失
            //使用此处直接捕获一下异常保证不会仅因skuName获取失败就回滚
            //TODO 还有另一种方法可以不回滚事务在高级篇优化
            try {
                R r = productFeignService.info(skuId);
                // 商品服务传过来的有用数据是一个Map<String,Object>
                Map<String,Object> skuInfo = (Map<String, Object>) r.get("skuInfo");
                System.out.println(skuInfo);
                if (r.getCode() == 0){
                    System.out.println((String) skuInfo.get("skuName"));
                    //查询成功 设置skuName 
                    wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.baseMapper.insert(wareSkuEntity);
        }else {
            //修改库存
            //自定义dao层方法
            this.baseMapper.addStock(skuId,wareId,skuNum);
        }

    }

}